<?php

namespace App\Models;

class WordDownloadLogs extends Base
{
    protected $table = 'word_download_logs';

    protected $fillable = [
        'id','word_id','user_id','status','created_at','updated_at'
    ];

    public function word(){
        return $this->belongsTo('App\Models\Words', 'word_id', 'id');
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort = 'created_at', $orderby = 'desc'){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['word_id']) && $params['word_id']) {
                        $query ->where('word_id',$params['word_id']);
                    }
                })->where('status','<>',-1)
//                ->with(['word' => function ($query) {
//                    $query->select(['id','title','format'])->get();
//                }])
                ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['id']) && $params['id']) {
                        $query ->whereIn('id',$params['id']);
                    }
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['word_id']) && $params['word_id']) {
                        $query ->where('word_id',$params['word_id']);
                    }
                })->where('status','<>',-1)
//                ->with(['word' => function ($query) {
//                    $query->select(['id','title','format'])->get();
//                }])
                ->orderBy($sort, $orderby)->get();
        }
        return $lists;
    }

}
